<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 异步操作一共有三种方式
    // promise/Generator/async
    // 解决了回调地狱、使得异步调用更加方便

    /* let p = new Promise(function (resolved, rejected) {
         // 执行异步操作
         let res = {
             code: 200,
             data: {
                 name: 'jack',
                 age: 21
             },
             error: '失败'
         }
         setTimeout(() => {
             if (res.code === 200) {
                 resolved(res.data)
             } else {
                 rejected(res.error)
             }
         }, 1000)
     })
     p.then(val => { // val 是resolved成功回来的值
         console.log(val)
     }).catch(err=>{
         console.log(err)
     })*/

    function timeOut(ms) {
        return new Promise((resolved, rejected) => {
            setTimeout(() => {
                resolved('hello promise success')
            },ms)
        })
    }

    timeOut(2000).then(val => {
        console.log(val)
    }).catch(err => {
        console.log(err)
    })

</script>
</body>
</html>
